﻿using Microsoft.Practices.Unity;
using MyCode.Project.Domain.Config;
using MyCode.Project.Domain.Message.Request.Report40;
using MyCode.Project.Domain.Message.Request.WebSocket;
using MyCode.Project.Domain.Message.Response.User;
using MyCode.Project.Domain.Message.Response.WebSocket;
using MyCode.Project.Infrastructure.Cache;
using MyCode.Project.Infrastructure.Common;
using MyCode.Project.Infrastructure.Constant;
using MyCode.Project.Infrastructure.Enumeration;
using MyCode.Project.Infrastructure.UnityExtensions;
using MyCode.Project.Infrastructure.WebPost;
using MyCode.Project.Repositories.Common;
using MyCode.Project.Services;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Fleck.Samples.ConsoleApp
{
    public class Server
    {
        static int procc = 0;
        static int maxPoolnum = 10;
        static void Main()
        {
            /// <summary>
            /// WebSocketIpAddress 
            /// </summary>
            IUnityContainer container = UnityHelper.GetUnityContainer();

            container.RegisterType<MyCodeSqlSugarClient>(new PerThreadLifetimeManager());

            new DingDingHelper(SystemConfig.DingDingApiUrl, "websocket");
            //注册缓存对象
            container.RegisterType<IMyCodeCacheService, RedisCache>(new InjectionConstructor(SystemConfig.RedisAddress, SystemConfig.CachePrefix));

            string webSocketIpAddress = WebConfigUtils.GetAppSettingsInfo("WebSocketIpAddress");

            //var _webSocketService = UnityHelper.GetService<IWebSocketService>();
            var _myCodeCacheService = UnityHelper.GetService<IMyCodeCacheService>();
            SystemCache systemCache = new SystemCache();
            FleckLog.Level = LogLevel.Debug;
            var allSockets = new List<IWebSocketConnection>();
            WebSocketMessgeResp<HomeMessgeResp> webSocketMessgeResp = new WebSocketMessgeResp<HomeMessgeResp>();
            webSocketMessgeResp.ContentTxt = new HomeMessgeResp();
            webSocketMessgeResp.ContentTxt.HeadImgUrl = "";
            webSocketMemberIdKey = new Dictionary<string, string>();

            //2021-12-29 本次版本运营要求取消消息页面，停止掉websocket消息
            while (1 == 0 && true)
            {
                try
                {
                    var server = new WebSocketServer(webSocketIpAddress);
                    server.Start(socket =>
                    {
                        socket.OnOpen = () =>
                        {
                            string name = "";
                            //解析授权token
                            if (socket.ConnectionInfo.Headers.Keys.Contains("Authorization"))
                            {
                                var authHeader = socket.ConnectionInfo.Headers["Authorization"];

                                if (!string.IsNullOrWhiteSpace(authHeader))
                                {
                                    try
                                    {
                                        var obj = TokenHelper.Get(authHeader.Trim(),
                                            SystemConfig.JwtKey,
                                            Const.LoginInfoKey);

                                        var loginInfo = ((JObject)obj).ToObject<MemberLoginInfo>();
                                        systemCache.Set(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort, loginInfo, new TimeSpan(365, 0, 0, 0));
                                        systemCache.Set("IP" + loginInfo.UserId.ToString(), socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort, new TimeSpan(365, 0, 0, 0));
                                        name = loginInfo.Name;
                                        LogHelper.Info(loginInfo.Name + "绑定IP:" + socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                        allSockets.Add(socket);
                                    }
                                    catch (Exception ex)
                                    {
                                        socket.Send("-2");
                                        Console.WriteLine(ex.Message);
                                        //socket.Close();

                                    }
                                }
                                Console.WriteLine(name + ":Open!");
                            }
                            else
                            {
                                Console.WriteLine("Open没有传授权参数");
                                LogHelper.Info("绑定IP:" + socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                socket.Send("-2");
                                Console.WriteLine("-2");
                                //socket.Close();
                            }

                            //socket.Send("链接成功");


                        };
                        socket.OnClose = () =>
                        {

                            allSockets.Remove(socket);
                            try
                            {
                                var loginInfo = systemCache.Get<MemberLoginInfo>(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                if (loginInfo != null)
                                {
                                    LogHelper.Info(loginInfo.Name + "删除IP:" + socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                    systemCache.Delete("IP" + loginInfo.UserId.ToString());
                                }
                                systemCache.Delete(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                systemCache.Delete("ShopId:" + loginInfo.UserId);
                                Console.WriteLine(loginInfo.Name + "Close!");
                            }
                            catch (Exception ex)
                            {

                                Console.WriteLine(ex.Message);
                                //socket.Close();

                            }

                        };
                        socket.OnMessage = message =>
                        {
                            var _webSocketService = UnityHelper.GetService<IWebSocketService>();
                            Console.WriteLine(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort + "--- " + message);
                            if (message == "ping") //心跳
                            {
                                socket.Send("pong");
                                Console.WriteLine("pong");
                                return;
                            }
                            LogHelper.Info("ip:" + socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                            var loginInfo = systemCache.Get<MemberLoginInfo>(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                            Console.WriteLine(message);
                            if (loginInfo != null)
                            {
                                var requst = JsonHelper.ToObject<MessgeRequst>(message);
                                string key = CacheKey.WebSocketQueueCacheKey + requst.Id + loginInfo.UserId;
                                if (requst.Message == "1")
                                {
                                    _myCodeCacheService.Delete(key + CacheKey.WebSocketMemberQueueCacheKey);
                                    _myCodeCacheService.Delete(key);
                                    if (!webSocketMemberIdKey.ContainsKey(loginInfo.UserId.ToString()))
                                    {
                                        webSocketMemberIdKey.Add(loginInfo.UserId.ToString(), key);
                                    }
                                    else
                                    {
                                        webSocketMemberIdKey[loginInfo.UserId.ToString()] = key;
                                    }
                                    LogHelper.Info(loginInfo.Name + " " + loginInfo.UserId + " 被增加了一个详情页的KEY:" + key);

                                }

                                if (webSocketMemberIdKey.ContainsKey(loginInfo.UserId.ToString()) && requst.Message == "0")
                                {
                                    int i = 0;
                                    //webSocketMemberIdKey[loginInfo.UserId.ToString()] = "";
                                    webSocketMemberIdKey.Remove(loginInfo.UserId.ToString());
                                    LogHelper.Info(loginInfo.Name + " " + loginInfo.UserId + " 被删了一个详情页的KEY:" + key);
                                    _myCodeCacheService.Delete(key);  //会员详情页消息
                                    _myCodeCacheService.Delete(key + CacheKey.WebSocketMemberQueueCacheKey);  //进店消息详情页通知
                                }
                                var result = _webSocketService.MessageProcessing(message, loginInfo);

                                if (!string.IsNullOrWhiteSpace(result))
                                {
                                    socket.Send(result);
                                    //Console.WriteLine(loginInfo.Name + result);
                                    LogHelper.Info(loginInfo.Name + $"接收消息后推送首页列表消息: " + result);
                                }

                                systemCache.Set("ShopId:" + loginInfo.UserId, requst.ShopId, new TimeSpan(1, 0, 0, 0));
                            }
                            else
                            {
                            }
                        };
                        socket.OnError = message =>
                        {

                            var loginInfo = systemCache.Get<MemberLoginInfo>(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                            Console.WriteLine(message);
                            systemCache.Delete(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                            if (loginInfo != null)
                            {
                                LogHelper.Error(loginInfo.Name + "出错啦!(148)");
                                Console.WriteLine(loginInfo.Name + "出错啦!");
                                DingDingSendMsg(loginInfo.Name + $"出错啦:" + message);
                                systemCache.Delete("IP" + loginInfo.UserId.ToString());
                            }
                            else
                            {
                                Console.WriteLine("出错啦!");
                                LogHelper.Error("(158)出错啦!");
                            }
                            LogHelper.Error(message);
                            //allSockets.Remove(socket);
                            //socket.Close();
                            //container.RegisterType<MyCodeSqlSugarClient>(new PerThreadLifetimeManager());
                            //Thread.Sleep(3000);
                        };

                    });
                    //监听消息队列里是否有新消息要推送

                    //详情消息
                    Task task1 = new Task(() =>
                    {
                        while (1 == 1)
                        {
                            try
                            {

                                var keys = webSocketMemberIdKey.Keys.ToList();
                                if (keys != null)
                                {
                                    foreach (var key in keys)
                                    {
                                        var keyValue = webSocketMemberIdKey[key];
                                        //LogHelper.Info($"keyValue:" + key);
                                        if (string.IsNullOrWhiteSpace(keyValue))
                                        {
                                            //LogHelper.Info($"消失的主动推详情消息KEY:" + key);
                                            continue;
                                        }
                                        var msg = (RedisMessge)_myCodeCacheService.Popup(keyValue);
                                        if (msg != null)
                                        {
                                            var clientIpInfo = systemCache.Get<string>("IP" + msg.UserId.ToString());
                                            if (clientIpInfo != null)
                                            {
                                                var socket = allSockets.FirstOrDefault(t => t.ConnectionInfo.ClientIpAddress + ":" + t.ConnectionInfo.ClientPort == clientIpInfo);
                                                if (socket != null)
                                                {
                                                    var loginInfo = systemCache.Get<MemberLoginInfo>(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                                    if (loginInfo != null)
                                                    {
                                                        var _webSocketService = UnityHelper.GetService<IWebSocketService>();
                                                        string result = _webSocketService.SendMsg(msg, loginInfo);
                                                        socket.Send(result);
                                                        LogHelper.Info(loginInfo.Name + $"主动推详情消息:" + result);
                                                        Console.WriteLine(loginInfo.Name + "主动推详情消息:" + result);
                                                        //LogHelper.Info("主动推详情消息:" + result);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                LogHelper.Info($"主动推详情消息没有IP:" + msg.UserId.ToString());
                                                Console.WriteLine("主动推详情消息没有IP:" + msg.UserId.ToString());
                                            }
                                        }
                                    };
                                }
                            }
                            catch (Exception ex1)
                            {
                                LogHelper.Info($"详情消息出错:" + ex1.Message);
                                LogHelper.Info(ex1);
                            }
                            Thread.Sleep(300);
                        }
                    });
                    task1.Start();
                    //首页消息
                    Task task2 = new Task(() =>
                     {
                         while (1 == 1)
                         {
                             try
                             {
                                 var IndexMsg = (RedisMessge)_myCodeCacheService.Popup(CacheKey.WebSocketQueueCacheKey);
                                 while (IndexMsg != null)
                                 {
                                     var clientIpInfo = systemCache.Get<string>("IP" + IndexMsg.UserId.ToString());
                                     if (clientIpInfo != null)
                                     {
                                         var socket = allSockets.FirstOrDefault(t => t.ConnectionInfo.ClientIpAddress + ":" + t.ConnectionInfo.ClientPort == clientIpInfo);
                                         if (socket != null)
                                         {
                                             var _webSocketService = UnityHelper.GetService<IWebSocketService>();
                                             var loginInfo = systemCache.Get<MemberLoginInfo>(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                                             IndexMsg.PositionType = 1;
                                             Guid? shopId = systemCache.Get<Guid?>("ShopId:" + loginInfo.UserId);
                                             if (shopId == null || (IndexMsg.ShopId != null && shopId == IndexMsg.ShopId) || IndexMsg.TemplateType == (int)ChatMessagType.MaterialsStore)
                                             {
                                                 string result = _webSocketService.SendMsg(IndexMsg, loginInfo);
                                                 shopId = systemCache.Get<Guid?>("ShopId:" + loginInfo.UserId);
                                                 if (shopId == null || (IndexMsg.ShopId != null && shopId == IndexMsg.ShopId) || IndexMsg.TemplateType == (int)ChatMessagType.MaterialsStore)
                                                 {
                                                     socket.Send(result);
                                                     LogHelper.Info(loginInfo.Name + $"主动推首页列表消息: " + result);
                                                     Console.WriteLine(loginInfo.Name + "主动推首页列表消息:" + result);
                                                 }
                                                 else
                                                 {
                                                     LogHelper.Info(shopId);
                                                     LogHelper.Info(IndexMsg);
                                                     LogHelper.Info(loginInfo.Name + "2主动推首页列表消息:不是当前店铺,不推此消息");
                                                     Console.WriteLine(loginInfo.Name + "2主动推首页列表消息:不是当前店铺,不推此消息");
                                                 }
                                             }
                                             else
                                             {
                                                 LogHelper.Info(shopId);
                                                 LogHelper.Info(IndexMsg);
                                                 LogHelper.Info(loginInfo.Name + "主动推首页列表消息:不是当前店铺,不推此消息");
                                                 Console.WriteLine(loginInfo.Name + "主动推首页列表消息:不是当前店铺,不推此消息");
                                             }

                                         }
                                     }
                                     else
                                     {
                                         LogHelper.Info($"主页列表消息:该用户没有在线: " + IndexMsg.UserId.ToString());
                                         Console.WriteLine("主页列表消息:该用户没有在线: " + IndexMsg.UserId.ToString());
                                     }
                                     IndexMsg = (RedisMessge)_myCodeCacheService.Popup(CacheKey.WebSocketQueueCacheKey);
                                 }
                                 //DingDingSendMsg($"这是一个轮回:" + DateTime.Now);
                                 //Console.WriteLine("这是一个轮回:" + DateTime.Now);
                             }
                             catch (Exception ex2)
                             {
                                 LogHelper.Info($"首页消息出错:" + ex2.Message);
                                 LogHelper.Info(ex2);
                             }
                             Thread.Sleep(300);
                         }
                     });
                    task2.Start();

                    ////会员进店提醒
                    //Task task3 = new Task(() =>
                    //{
                    //    while (1 == 1)
                    //    {
                    //        try
                    //        {
                    //            var keys = webSocketMemberIdKey.Keys.ToList();
                    //            if (keys != null)
                    //            {
                    //                foreach (var key in keys)
                    //                {
                    //                    var keyValue = webSocketMemberIdKey[key];
                    //                    if (string.IsNullOrWhiteSpace(keyValue))
                    //                        continue;
                    //                    var msg = (RedisMessge)_myCodeCacheService.Popup(keyValue + CacheKey.WebSocketMemberQueueCacheKey);
                    //                    if (msg != null)
                    //                    {
                    //                        var clientIpInfo = systemCache.Get<string>("IP" + msg.UserId.ToString());
                    //                        if (clientIpInfo != null)
                    //                        {
                    //                            var socket = allSockets.FirstOrDefault(t => t.ConnectionInfo.ClientIpAddress + ":" + t.ConnectionInfo.ClientPort == clientIpInfo);
                    //                            if (socket != null)
                    //                            {
                    //                                var loginInfo = systemCache.Get<MemberLoginInfo>(socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort);
                    //                                if (loginInfo != null)
                    //                                {
                    //                                    var _webSocketService = UnityHelper.GetService<IWebSocketService>();
                    //                                    string result = _webSocketService.SendMsg(msg, loginInfo);
                    //                                    socket.Send(result);
                    //                                    LogHelper.Info(loginInfo.Name + $"主动推会员进店详情消息:" + result);
                    //                                    Console.WriteLine(loginInfo.Name + "主动推会员进店详情消息:" + result);
                    //                                    //LogHelper.Info("主动推详情消息:" + result);
                    //                                }
                    //                            }
                    //                        }
                    //                        else
                    //                        {
                    //                            LogHelper.Info($"主动推会员进店详情消息没有IP:" + msg.UserId.ToString());
                    //                            Console.WriteLine("主动推会员进店详情消息没有IP:" + msg.UserId.ToString());
                    //                        }
                    //                    }
                    //                };
                    //            }
                    //        }
                    //        catch (Exception ex3)
                    //        {
                    //            LogHelper.Info($"会员进店提醒出错:" + ex3.Message);
                    //            LogHelper.Info(ex3);
                    //        }
                    //        Thread.Sleep(1000);
                    //    }
                    //});
                    //task3.Start();
                    ////每秒轮询调用会员进店提醒方法
                    //Task task4 = new Task(() =>
                    //{   
                    //    while (1 == 1)
                    //    {
                    //        DateTime time = DateTime.Now;
                    //        var _webSocketService = UnityHelper.GetService<IWebSocketService>();
                    //        try
                    //        {
                    //            searchTime = _webSocketService.SendMessageByFace(searchTime);
                    //        }
                    //        catch(Exception ex4)
                    //        {
                    //            LogHelper.Info($"每秒轮询调用会员进店提醒方法出错:" + ex4.Message);
                    //            LogHelper.Info(ex4);
                    //        }
                    //        var span = DateTime.Now - time;
                    //        if (1000d - span.TotalMilliseconds > 0)
                    //        {
                    //            var sss =(int)( 1000 - span.TotalMilliseconds);
                    //            Thread.Sleep(sss);
                    //        }

                    //    }
                    //});
                    //task4.Start();
                    Console.ReadLine();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex);
                    LogHelper.Error("(238)出错:" + ex.Message);
                    LogHelper.Error(ex);
                    DingDingSendMsg($"(240)出错啦:" + ex.Message);
                    allSockets.Clear();
                    allSockets = new List<IWebSocketConnection>();

                    Thread.Sleep(300);
                }
            }

            int send = 1;
            int hour = 0;
            
            //把门店的使用数据储存到数据库
            while (true)
            {
                try
                {
                   
                    ThreadPool.SetMinThreads(1, 1);
                    ThreadPool.SetMaxThreads(maxPoolnum, maxPoolnum);
                    if (DateTime.Now.Hour == hour && send == 1)
                    {
                        send = 0;
                        DingDingSendMsg("正常运行中: " + DateTime.Now);
                    }
                    else if (DateTime.Now.Hour != hour)
                    {
                        hour = DateTime.Now.Hour;
                        send = 1;
                    }
                    Console.WriteLine("憨鸠鸠又一日 " + DateTime.Now);
                    procc++;
                    var abc = ThreadPool.QueueUserWorkItem(new WaitCallback(rpShopUseAppletDataServiceSaveData), 1);
                    //var _rpShopUseAppletDataService = UnityHelper.GetService<IRpShopUseAppletDataService>();
                    //var temp1 = _myCodeCacheService.Popup(CacheKey.RpShopUseAppletDataKey2);
                    //if (temp1 != null)
                    //{
                    //    var something = _myCodeCacheService.Popup(CacheKey.RpShopUseAppletDataKey);
                    //    if (something != null)
                    //    {
                    //        string json = JsonHelper.ToJson(something);
                    //        Console.WriteLine("获取到的参数 :" + json);
                    //        if (!string.IsNullOrWhiteSpace(json))
                    //            _rpShopUseAppletDataService.SaveData(json);
                    //    }
                    //    var req1 = JsonHelper.ToObject<RpShopUseAppletDataAct>(temp1.ToString());
                    //    _rpShopUseAppletDataService.SelectSetData(req1.ApiUrl, req1.RequestDataStr, req1.Token);

                    //}
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    LogHelper.Error("(452)出错:" + ex.Message);
                    LogHelper.Error(ex);
                    DingDingSendMsg($"(454)出错啦:" + ex.Message);
                }
                Thread.Sleep(1000);

                try
                {
                    var _HuLiHuShao40Service = UnityHelper.GetService<IHuLiHuShao40Service>();
                    var something = _myCodeCacheService.Popup(CacheKey.RpHuLiHuShao);
                    if (something != null)
                    {
                        string json = JsonHelper.ToJson(something);
                        Console.WriteLine("年报月报获取到的参数 :" + json);
                        if (!string.IsNullOrWhiteSpace(json))
                            _HuLiHuShao40Service.AutoSetReport();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    LogHelper.Error("(471)年报月报出错:" + ex.Message);
                    LogHelper.Error(ex);
                    DingDingSendMsg($"(473)年报月报出错:" + ex.Message);
                }

                try
                {

                    if (_myCodeCacheService.Exists("Auto:GetShopFenXiReport2207"))
                    {
                        var tempJson = _myCodeCacheService.Get<string>("Auto:GetShopFenXiReport2207");
                        var templist = JsonHelper.ToObject<List<GetShopFenXiReport2207>>(tempJson);
                        _myCodeCacheService.Delete("Auto:GetShopFenXiReport2207");
                        var abc = ThreadPool.QueueUserWorkItem(new WaitCallback(AddPool), templist);
                        //templist.ForEach(t =>
                        //{                          
                        //    var abc = ThreadPool.QueueUserWorkItem(new WaitCallback(TaskFenXiReportToCache), t);                            
                        //});
                        Console.WriteLine("执行提前查询方法：Auto:GetShopFenXiReport2207");

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                try
                {

                    if (_myCodeCacheService.Exists("Auto:PoolTaskSave2308TodayData"))
                    {
                        var tempJson = _myCodeCacheService.Popup("Auto:PoolTaskSave2308TodayData");                  
                       
                        var abc = ThreadPool.QueueUserWorkItem(new WaitCallback(PoolTaskSave2308TodayData), tempJson);
                        
                        Console.WriteLine("执行提前查询方法：Auto:PoolTaskSave2308TodayData");

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }

        //private static ConcurrentBag<string> webSocketMemberIdKey;
        private static Dictionary<string, string> webSocketMemberIdKey;
        private static DateTime searchTime = DateTime.Now;

        #region SendMsg(发送信息)
        private static Task DingDingSendMsg(string content)
        {
            string url = WebConfigUtils.GetAppSettingsInfo("DingDingApiUrl");
            string _txt = "websocket" + WebConfigUtils.GetAppSettingsInfo("DingDingTxt");
            WebUtils webUtils = new WebUtils();
            var jsonObject = new { msgtype = "text", text = new { content = _txt + content } };

            var jsonStr = JsonHelper.ToJson(jsonObject);
            try
            {
                var result = webUtils.DoPostJson(url, jsonStr);
            }
            catch
            { }

            return Task.CompletedTask;

        }
        #endregion

        public static void TaskFenXiReportToCache(object i)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                GetShopFenXiReport2207 act = (GetShopFenXiReport2207)i;
                Console.WriteLine($@"{procc}线程池执行方法：TaskFenXiReportToCache 开始" + act.UserId + "  " + act.DuiBi + " 线程ID：" + Thread.CurrentThread.ManagedThreadId);
                var _report2207Service = UnityHelper.GetService<IReport2207Service>();
                sw.Start();
                _report2207Service.SetFenXiReportToCache(act.UserId, act.DuiBi);
                sw.Stop();
                Console.WriteLine($@"{procc}线程池执行方法：TaskFenXiReportToCache  结束,耗时" + sw.Elapsed.TotalSeconds + "秒" + act.UserId + "  " + act.DuiBi + " 线程ID：" + Thread.CurrentThread.ManagedThreadId);
                procc--;
            }
            catch (Exception ex)
            {
                procc--;
                Console.WriteLine(ex);
            }

        }

        public static void rpShopUseAppletDataServiceSaveData(object i)
        {
            try
            {
                Console.WriteLine(procc + "线程池执行方法：rpShopUseAppletDataServiceSaveData 开始" + " 线程ID：" + Thread.CurrentThread.ManagedThreadId);
                Stopwatch sw = new Stopwatch();
                sw.Start();
                var _myCodeCacheService = UnityHelper.GetService<IMyCodeCacheService>();
                var _rpShopUseAppletDataService = UnityHelper.GetService<IRpShopUseAppletDataService>();
                var temp1 = _myCodeCacheService.Popup(CacheKey.RpShopUseAppletDataKey2);
                if (temp1 != null)
                {
                    var something = _myCodeCacheService.Popup(CacheKey.RpShopUseAppletDataKey);
                    if (something != null)
                    {
                        string json = JsonHelper.ToJson(something);
                        Console.WriteLine("获取到的参数 :" + json);
                        if (!string.IsNullOrWhiteSpace(json))
                            _rpShopUseAppletDataService.SaveData(json);
                    }
                    var req1 = JsonHelper.ToObject<RpShopUseAppletDataAct>(temp1.ToString());
                    _rpShopUseAppletDataService.SelectSetData(req1.ApiUrl, req1.RequestDataStr, req1.Token);

                }
                sw.Stop();
                Console.WriteLine(procc + "线程池执行方法：rpShopUseAppletDataServiceSaveData  结束,耗时" + sw.Elapsed.TotalSeconds + "秒" + " 线程ID：" + Thread.CurrentThread.ManagedThreadId);
                procc--;
            }
            catch (Exception ex)
            {
                procc--;
                Console.WriteLine(ex);
            }
        }

        public static void AddPool(object act)
        {
            List<GetShopFenXiReport2207> templist = (List<GetShopFenXiReport2207>)act;
            templist.ForEach(t =>
            {
                procc++;
                while (procc >= (maxPoolnum-2))
                {
                    Thread.Sleep(1000);
                }
                var abc = ThreadPool.QueueUserWorkItem(new WaitCallback(TaskFenXiReportToCache), t);
            });
        }

        public static void PoolTaskSave2308TodayData(object i)
        {
            try
            {
                Console.WriteLine(procc + "线程池执行方法：PoolTaskSave2308TodayData 开始" + " 线程ID：" + Thread.CurrentThread.ManagedThreadId);
                Stopwatch sw = new Stopwatch();
                sw.Start();

                var _report2308Service = UnityHelper.GetService<IReport2308Service>();
                _report2308Service.TaskSave2308TodayData(Guid.Parse(i.ToString()));
                sw.Stop();
                Console.WriteLine(procc + "线程池执行方法：PoolTaskSave2308TodayData  结束,耗时" + sw.Elapsed.TotalSeconds + "秒" + " 线程ID：" + Thread.CurrentThread.ManagedThreadId);
                procc--;
            }
            catch (Exception ex)
            {
                    procc--;
                    Console.WriteLine(ex);
            }
        }
         
    }
}
